﻿using System;
using System.Data;
using System.Configuration;
using System.Linq;
using HUBS1.HCMS.Entity;
using HCMS.Utility;

namespace HUBS1.HCMS.Logic
{

    //public enum PlanLevel { Hotel = 1, Room, Plan }

    /// <summary>
    /// 房量数据转换类
    /// Jay.Gu
    /// 2009-04-10
    /// </summary>
    public class HotelAIProvider
    {
        private DataSet _ds1_AI = new DataSet();
        private DataSet _ds2_AI = new DataSet();
        private DataSet _ds3_AI = new DataSet();
        private DataSet _ds4_AI = new DataSet();
        private DataSet _ds5_AI = new DataSet();
        private DataSet _ds6_AI = new DataSet();
        private DataSet _ds7_AI = new DataSet();
        private DataSet _ds8_AI = new DataSet();
        private DataSet _ds9_AI = new DataSet();
        private DataSet _ds10_AI = new DataSet();
        private DataSet _ds11_AI = new DataSet();
        private DataSet _ds12_AI = new DataSet();

        private DataSetUtility _utility = new DataSetUtility();

        private int _iYear;

        /// <summary>
        /// 年
        /// </summary>
        public int Year
        {
            get
            {
                return _iYear;
            }
            set
            {
                _iYear = value;
            }
        }


        #region 每月DataSet数据

        public DataSet January
        {
            get
            {
                return _ds1_AI;
            }
        }

        public DataSet February
        {
            get
            {
                return _ds2_AI;
            }
        }
        public DataSet March
        {
            get
            {
                return _ds3_AI;
            }
        }
        public DataSet April
        {
            get
            {
                return _ds4_AI;
            }
        }
        public DataSet May
        {
            get
            {
                return _ds5_AI;
            }
        }
        public DataSet June
        {
            get
            {
                return _ds6_AI;
            }
        }

        public DataSet July
        {
            get
            {
                return _ds7_AI;
            }
        }

        public DataSet August
        {
            get
            {
                return _ds8_AI;
            }
        }

        public DataSet September
        {
            get
            {
                return _ds9_AI;
            }
        }

        public DataSet October
        {
            get
            {
                return _ds10_AI;
            }
        }

        public DataSet November
        {
            get
            {
                return _ds11_AI;
            }
        }

        public DataSet December
        {
            get
            {
                return _ds12_AI;
            }
        }

        #endregion

        /// <summary>
        /// 按日期范围加载, 日期必须同一年内
        /// </summary>
        /// <param name="StartDate">开始日期</param>
        /// <param name="EndDate">结束日期</param>
        /// <param name="Rate1">单人价格</param>
        /// <param name="Rate2">双人价格</param>
        /// <param name="Rate3">3人价</param>
        /// <param name="Rate4">4人价</param>
        /// <param name="Week">每天的有效性</param>
        /// <param name="State">房间状态</param>
        /// <param name="Min">最小入住限制</param>
        /// <param name="Max">最大入住限制</param>
        /// <param name="Inventory">房量</param>
        /// <param name="Gua">保证金制度</param>
        /// <param name="Cxl">取消政策</param>
        /// <param name="Advance">提前预订天数</param>
        public void Create(DateTime StartDate, DateTime EndDate, int[] Week,
                           int Inventory, string State, int Min, int Max, int Advance, string Gua, string Cxl)
        {

            if (EndDate <= StartDate)
                return;

            //不允许跨年
            if (StartDate.Year != EndDate.Year)
                return;

            _iYear = StartDate.Year;

            //int iMonth = EndDate.Year * 12 + EndDate.Month - StartDate.Year * 12 - StartDate.Month;

            TimeSpan span = EndDate - StartDate;

            for (int i = 0; i <= span.Days; i++)
            {
                DateTime day = StartDate.AddDays(i);

                if (!SkipDate(day, Week))
                {
                    #region 写入DataSet

                    switch (day.Month)
                    {
                        case 1:

                            if (_ds1_AI == null)
                                _ds1_AI = new DataSet();

                            AddAI(_ds1_AI, day, Inventory, State, Min, Max, Advance, Gua, Cxl);

                            break;

                        case 2:

                            if (_ds2_AI == null)
                                _ds2_AI = new DataSet();
                            AddAI(_ds2_AI, day, Inventory, State, Min, Max, Advance, Gua, Cxl);

                            break;

                        case 3:

                            if (_ds3_AI == null)
                                _ds3_AI = new DataSet();
                            AddAI(_ds3_AI, day, Inventory, State, Min, Max, Advance, Gua, Cxl);

                            break;

                        case 4:

                            if (_ds4_AI == null)
                                _ds4_AI = new DataSet();
                            AddAI(_ds4_AI, day, Inventory, State, Min, Max, Advance, Gua, Cxl);

                            break;
                        case 5:

                            if (_ds5_AI == null)
                                _ds5_AI = new DataSet();
                            AddAI(_ds5_AI, day, Inventory, State, Min, Max, Advance, Gua, Cxl);

                            break;
                        case 6:

                            if (_ds6_AI == null)
                                _ds6_AI = new DataSet();
                            AddAI(_ds6_AI, day, Inventory, State, Min, Max, Advance, Gua, Cxl);

                            break;
                        case 7:

                            if (_ds7_AI == null)
                                _ds7_AI = new DataSet();
                            AddAI(_ds7_AI, day, Inventory, State, Min, Max, Advance, Gua, Cxl);

                            break;

                        case 8:

                            if (_ds8_AI == null)
                                _ds8_AI = new DataSet();
                            AddAI(_ds8_AI, day, Inventory, State, Min, Max, Advance, Gua, Cxl);

                            break;

                        case 9:

                            if (_ds9_AI == null)
                                _ds9_AI = new DataSet();
                            AddAI(_ds9_AI, day, Inventory, State, Min, Max, Advance, Gua, Cxl);

                            break;

                        case 10:

                            if (_ds10_AI == null)
                                _ds10_AI = new DataSet();

                            AddAI(_ds10_AI, day, Inventory, State, Min, Max, Advance, Gua, Cxl);

                            break;

                        case 11:

                            if (_ds11_AI == null)
                                _ds11_AI = new DataSet();

                            AddAI(_ds11_AI, day, Inventory, State, Min, Max, Advance, Gua, Cxl);

                            break;

                        case 12:

                            if (_ds12_AI == null)
                                _ds12_AI = new DataSet();

                            AddAI(_ds12_AI, day, Inventory, State, Min, Max, Advance, Gua, Cxl);

                            break;
                    }

                    #endregion
                }

            }

        }

        /// <summary>
        /// 检查日期是否需要跳过
        /// </summary>
        /// <param name="Day"></param>
        /// <param name="Week"></param>
        /// <returns></returns>
        private bool SkipDate(DateTime Day, int[] Week)
        {
            try
            {
                if (Day.DayOfWeek == DayOfWeek.Monday)
                {
                    if (Week[0] == 0)
                        return true;
                }
                else if (Day.DayOfWeek == DayOfWeek.Tuesday)
                {
                    if (Week[1] == 0)
                        return true;
                }
                else if (Day.DayOfWeek == DayOfWeek.Wednesday)
                {
                    if (Week[2] == 0)
                        return true;
                }
                else if (Day.DayOfWeek == DayOfWeek.Thursday)
                {
                    if (Week[3] == 0)
                        return true;
                }
                else if (Day.DayOfWeek == DayOfWeek.Friday)
                {
                    if (Week[4] == 0)
                        return true;
                }
                else if (Day.DayOfWeek == DayOfWeek.Saturday)
                {
                    if (Week[5] == 0)
                        return true;
                }
                else if (Day.DayOfWeek == DayOfWeek.Sunday)
                {
                    if (Week[6] == 0)
                        return true;
                }
            }
            catch
            {

            }

            return false;

        }

        /// <summary>
        /// 修改每月AI字段值
        /// </summary>
        private void AddAI(DataSet ds, DateTime StartDate, int Inventory, string State, int Min, int Max, int Adv, string Gua, string Cxl)
        {
            CreateDataSet(ds);

            DataRow[] rows = ds.Tables[0].Select("Date = '" + StartDate.ToString("yyyy-MM-dd") + "'");

            if (rows.Length == 0)
            {
                DataRow dr = ds.Tables[0].NewRow();

                dr["Date"] = StartDate;
                dr["Inventory"] = Inventory;
                dr["State"] = State;
                dr["Min"] = Min;
                dr["Max"] = Max;
                dr["Adv"] = Adv;

                dr["Gua"] = Gua;
                dr["Cxl"] = Cxl;

                ds.Tables[0].Rows.Add(dr);
            }
            else
            {
                rows[0]["Inventory"] = Inventory;
                rows[0]["State"] = State;
                rows[0]["Min"] = Min;
                rows[0]["Max"] = Max;
                rows[0]["Adv"] = Adv;

                rows[0]["Gua"] = Gua;
                rows[0]["Cxl"] = Cxl;
            }
        }

        /// <summary>
        /// 初始化DataSet
        /// </summary>
        /// <param name="ds"></param>
        protected void CreateDataSet(DataSet ds)
        {
            if (ds.Tables.Count == 0)
            {
                DataTable dt = new DataTable();

                dt.Columns.Add("Date", typeof(DateTime));

                dt.Columns.Add("Inventory", typeof(int));
                dt.Columns.Add("State", typeof(string));
                dt.Columns.Add("Min", typeof(int));
                dt.Columns.Add("Max", typeof(int));
                dt.Columns.Add("Adv", typeof(int));

                dt.Columns.Add("Gua", typeof(string));
                dt.Columns.Add("Cxl", typeof(string));

                ds.Tables.Add(dt);
            }

        }

        /// <summary>
        /// 加载数据
        /// </summary>
        /// <param name="AIR">AIR对象</param>
        public void Load(HCRS_AIR AIR)
        {
            _iYear = AIR.Year.Value;

            //反序列化

            if (AIR.January_AI != null)
                _ds1_AI = _utility.RetrieveDataSet(AIR.January_AI);

            if (AIR.February_AI != null)
                _ds2_AI = _utility.RetrieveDataSet(AIR.February_AI);

            if (AIR.March_AI != null)
                _ds3_AI = _utility.RetrieveDataSet(AIR.March_AI);

            if (AIR.April_AI != null)
                _ds4_AI = _utility.RetrieveDataSet(AIR.April_AI);

            if (AIR.May_AI != null)
                _ds5_AI = _utility.RetrieveDataSet(AIR.May_AI);

            if (AIR.June_AI != null)
                _ds6_AI = _utility.RetrieveDataSet(AIR.June_AI);

            if (AIR.July_AI != null)
                _ds7_AI = _utility.RetrieveDataSet(AIR.July_AI);

            if (AIR.August_AI != null)
                _ds8_AI = _utility.RetrieveDataSet(AIR.August_AI);

            if (AIR.September_AI != null)
                _ds9_AI = _utility.RetrieveDataSet(AIR.September_AI);

            if (AIR.October_AI != null)
                _ds10_AI = _utility.RetrieveDataSet(AIR.October_AI);

            if (AIR.November_AI != null)
                _ds11_AI = _utility.RetrieveDataSet(AIR.November_AI);

            if (AIR.December_AI != null)
                _ds12_AI = _utility.RetrieveDataSet(AIR.December_AI);

        }


        /// <summary>
        /// 输出byte数组
        /// </summary>
        /// <param name="Month">月份</param>
        /// <returns></returns>
        public byte[] ConvertToBinaryData(int Month)
        {
            switch (Month)
            {
                case 1:
                    if (_ds1_AI == null)
                        return null;

                    return _utility.GetBinaryFormatData(_ds1_AI);
                case 2:
                    if (_ds2_AI == null)
                        return null;

                    return _utility.GetBinaryFormatData(_ds2_AI);
                case 3: 
                    if (_ds3_AI == null)
                        return null;

                    return _utility.GetBinaryFormatData(_ds3_AI);
                case 4:
                    if (_ds4_AI == null)
                        return null;

                    return _utility.GetBinaryFormatData(_ds4_AI);
                case 5:
                    if (_ds5_AI == null)
                        return null;

                    return _utility.GetBinaryFormatData(_ds5_AI);
                case 6:
                    if (_ds6_AI == null)
                        return null;

                    return _utility.GetBinaryFormatData(_ds6_AI);
                case 7:
                    if (_ds7_AI == null)
                        return null;

                    return _utility.GetBinaryFormatData(_ds7_AI);
                case 8:
                    if (_ds8_AI == null)
                        return null;

                    return _utility.GetBinaryFormatData(_ds8_AI);
                case 9:
                    if (_ds9_AI == null)
                        return null;

                    return _utility.GetBinaryFormatData(_ds9_AI);
                case 10:
                    if (_ds10_AI == null)
                        return null;

                    return _utility.GetBinaryFormatData(_ds10_AI);
                case 11:
                    if (_ds11_AI == null)
                        return null;

                    return _utility.GetBinaryFormatData(_ds11_AI);
                case 12:
                    if (_ds12_AI == null)
                        return null;

                    return _utility.GetBinaryFormatData(_ds12_AI);

            }

            return null;
        }


        /// <summary>
        /// 获取某天的AI记录
        /// </summary>
        /// <param name="Date">日期</param>
        /// <returns></returns>
        public DataRow GetRow(DateTime Date)
        {

            DataRow[] drSearch = null;

            switch (Date.Month)
            {
                case 1:

                    if (_ds1_AI.Tables.Count == 0) return null;

                    drSearch = _ds1_AI.Tables[0].Select("Date = '" + Date.ToString("yyyy-MM-dd") + "'");

                    if (drSearch.Length > 0)
                        return drSearch[0];

                    break;

                case 2:
                    if (_ds2_AI.Tables.Count == 0) return null;
                    drSearch = _ds2_AI.Tables[0].Select("Date = '" + Date.ToString("yyyy-MM-dd") + "'");

                    if (drSearch.Length > 0)
                        return drSearch[0];

                    break;

                case 3:
                    if (_ds3_AI.Tables.Count == 0) return null;
                    drSearch = _ds3_AI.Tables[0].Select("Date = '" + Date.ToString("yyyy-MM-dd") + "'");

                    if (drSearch.Length > 0)
                        return drSearch[0];

                    break;

                case 4:
                    if (_ds4_AI.Tables.Count == 0) return null;
                    drSearch = _ds4_AI.Tables[0].Select("Date = '" + Date.ToString("yyyy-MM-dd") + "'");

                    if (drSearch.Length > 0)
                        return drSearch[0];

                    break;

                case 5:
                    if (_ds5_AI.Tables.Count == 0) return null;
                    drSearch = _ds5_AI.Tables[0].Select("Date = '" + Date.ToString("yyyy-MM-dd") + "'");

                    if (drSearch.Length > 0)
                        return drSearch[0];

                    break;

                case 6:
                    if (_ds6_AI.Tables.Count == 0) return null;
                    drSearch = _ds6_AI.Tables[0].Select("Date = '" + Date.ToString("yyyy-MM-dd") + "'");

                    if (drSearch.Length > 0)
                        return drSearch[0];

                    break;

                case 7:
                    if (_ds7_AI.Tables.Count == 0) return null;
                    drSearch = _ds7_AI.Tables[0].Select("Date = '" + Date.ToString("yyyy-MM-dd") + "'");

                    if (drSearch.Length > 0)
                        return drSearch[0];

                    break;

                case 8:
                    if (_ds8_AI.Tables.Count == 0) return null;
                    drSearch = _ds8_AI.Tables[0].Select("Date = '" + Date.ToString("yyyy-MM-dd") + "'");

                    if (drSearch.Length > 0)
                        return drSearch[0];

                    break;

                case 9:
                    if (_ds9_AI.Tables.Count == 0) return null;
                    drSearch = _ds9_AI.Tables[0].Select("Date = '" + Date.ToString("yyyy-MM-dd") + "'");

                    if (drSearch.Length > 0)
                        return drSearch[0];

                    break;

                case 10:
                    if (_ds10_AI.Tables.Count == 0) return null;
                    drSearch = _ds10_AI.Tables[0].Select("Date = '" + Date.ToString("yyyy-MM-dd") + "'");

                    if (drSearch.Length > 0)
                        return drSearch[0];

                    break;

                case 11:
                    if (_ds11_AI.Tables.Count == 0) return null;
                    drSearch = _ds11_AI.Tables[0].Select("Date = '" + Date.ToString("yyyy-MM-dd") + "'");

                    if (drSearch.Length > 0)
                        return drSearch[0];

                    break;

                case 12:
                    if (_ds12_AI.Tables.Count == 0) return null;
                    drSearch = _ds12_AI.Tables[0].Select("Date = '" + Date.ToString("yyyy-MM-dd") + "'");

                    if (drSearch.Length > 0)
                        return drSearch[0];

                    break;

            }

            return null;
        }

        /// <summary>
        /// 获取某月的房量数组
        /// 数组下标为日期号 - 1,
        /// 用于日历报表显示
        /// </summary>
        /// <param name="Month">月份</param>
        /// <returns></returns>
        public int[] GetInventoryList(int Month)
        {
            if (_iYear > 0)
            {
                switch (Month)
                {
                    case 1:
                        return ConvertInventoryToInt(_ds1_AI, Month);
                    case 2:
                        return ConvertInventoryToInt(_ds2_AI, Month);
                    case 3:
                        return ConvertInventoryToInt(_ds3_AI, Month);
                    case 4:
                        return ConvertInventoryToInt(_ds4_AI, Month);
                    case 5:
                        return ConvertInventoryToInt(_ds5_AI, Month);
                    case 6:
                        return ConvertInventoryToInt(_ds6_AI, Month);
                    case 7:
                        return ConvertInventoryToInt(_ds7_AI, Month);
                    case 8:
                        return ConvertInventoryToInt(_ds8_AI, Month);
                    case 9:
                        return ConvertInventoryToInt(_ds9_AI, Month);
                    case 10:
                        return ConvertInventoryToInt(_ds10_AI, Month);
                    case 11:
                        return ConvertInventoryToInt(_ds11_AI, Month);
                    case 12:
                        return ConvertInventoryToInt(_ds12_AI, Month);
                }
            }

            return null;
        }

        public string[] GetStatusList(int Month)
        {
            if (_iYear > 0)
            {
                switch (Month)
                {
                    case 1:
                        return ConvertStateToString(_ds1_AI, Month);
                    case 2:
                        return ConvertStateToString(_ds2_AI, Month);
                    case 3:
                        return ConvertStateToString(_ds3_AI, Month);
                    case 4:
                        return ConvertStateToString(_ds4_AI, Month);
                    case 5:
                        return ConvertStateToString(_ds5_AI, Month);
                    case 6:
                        return ConvertStateToString(_ds6_AI, Month);
                    case 7:
                        return ConvertStateToString(_ds7_AI, Month);
                    case 8:
                        return ConvertStateToString(_ds8_AI, Month);
                    case 9:
                        return ConvertStateToString(_ds9_AI, Month);
                    case 10:
                        return ConvertStateToString(_ds10_AI, Month);
                    case 11:
                        return ConvertStateToString(_ds11_AI, Month);
                    case 12:
                        return ConvertStateToString(_ds12_AI, Month);
                }
            }

            return null;
        }

        private int[] ConvertInventoryToInt(DataSet ds, int Month)
        {
            int[] iInventory;

            DateTime dtDay = Convert.ToDateTime(_iYear.ToString() + "-" + Month.ToString() + "-01");

            int iSize = 0;

            while (dtDay.Month == Month)
            {
                iSize++;

                dtDay = dtDay.AddDays(1);
            }

            iSize++;

            iInventory = new int[iSize];

            for (int i = 0; i < iSize; i++)
                iInventory[i] = 0;

            if (ds.Tables.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    DateTime dtDate = Convert.ToDateTime(dr["Date"]);

                    int iNum = 0;

                    int.TryParse(dr["Inventory"].ToString(), out iNum);

                    iInventory[dtDate.Day - 1] = iNum;

                }
            }

            return iInventory;
        }


        private string[] ConvertStateToString(DataSet ds, int Month)
        {
            string[] strState;

            DateTime dtDay = Convert.ToDateTime(_iYear.ToString() + "-" + Month.ToString() + "-01");

            int iSize = 0;

            while (dtDay.Month == Month)
            {
                iSize++;

                dtDay = dtDay.AddDays(1);
            }

            iSize++;

            strState = new string[iSize];

            if (ds.Tables.Count > 0)
            {
                foreach (DataRow dr in ds.Tables[0].Rows)
                {
                    DateTime dtDate = Convert.ToDateTime(dr["Date"]);

                    strState[dtDate.Day - 1] = dr["State"].ToString();

                }
            }

            return strState;
        }
    }
}
